<?php
class ModelModuleBossBoughtTogether extends Model {
    public function checkBoughtTogether() {       
        $create_product_bought_together = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "product_bought_together` (`product_id` int(11) NOT NULL, `bought_together_id` int(11) NOT NULL, PRIMARY KEY  (`product_id`, `bought_together_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
		$this->db->query($create_product_bought_together);
	}
    
    public function getProductBoughtTogether($product_id) {
		$product_boughttogether_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_bought_together WHERE product_id = '" . (int)$product_id . "'");
		
		foreach ($query->rows as $result) {
			$product_boughttogether_data[] = $result['bought_together_id'];
		}
		
		return $product_boughttogether_data;
	}
    
    public function editBoughtTogether($product_id, $data) {

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_bought_together WHERE product_id = '" . (int)$product_id . "'");

		if (isset($data['product_boughttogether'])) {
			foreach ($data['product_boughttogether'] as $bought_together_id) {
			     if($bought_together_id != $product_id){
			         $this->db->query("DELETE FROM " . DB_PREFIX . "product_bought_together WHERE product_id = '" . (int)$product_id . "' AND bought_together_id = '" . (int)$bought_together_id . "'");
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_bought_together SET product_id = '" . (int)$product_id . "', bought_together_id = '" . (int)$bought_together_id . "'");
			     }
				
			}
		}
		
	}
    
    public function dropProductBoughtTogether(){
        $this->db->query("DROP TABLE `" . DB_PREFIX . "product_bought_together`");
    }
}
?>